﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ISRecord.View.Services;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Data;
using ISRecord.View.Properties;
using System.Windows;
using GalaSoft.MvvmLight.Messaging;
namespace ISRecord.View.ViewsModel
{
    public class ShellViewModel:WorkspaceViewModel
    {
        #region "Fields"
        ObservableCollection<WorkspaceViewModel> workspaces;
        ReadOnlyCollection<CommandViewModel> commands;
        CommandViewModel outboxRecordCommand;
        CommandViewModel allOutobxesCommand;
        CommandViewModel bulkOutboxesCommand;
        CommandViewModel settingsCommand;
        #endregion

      
    
        #region "Commands"
        public CommandViewModel OutboxRecordCommand
        {
            get
            {
                if (outboxRecordCommand == null)
                {
                    outboxRecordCommand = new CommandViewModel(Resources.MainWindow_Command_OutboxRecord,
                                                            new DelegateCommand(param => ShowOutboxRecord()));
                }
                return outboxRecordCommand;
            }
        }
        public CommandViewModel AllOutboxesCommand
        {
            get
            {
                if (allOutobxesCommand == null)
                {
                    allOutobxesCommand = new CommandViewModel(Resources.MainWidow_Command_ShowAllOutboxes,
                                                            new DelegateCommand(param => ShowAllOutboxes()));
                }
                return allOutobxesCommand;
            }
        }
        public CommandViewModel BulkOutboxesCommand
        {
            get
            {
                if (bulkOutboxesCommand == null)
                {
                    bulkOutboxesCommand = new CommandViewModel(Resources.MainWindow_Command_BulkOutboxes,
                                                            new DelegateCommand(param => ShowBulkOutboxes()));
                }
                return bulkOutboxesCommand;
            }
        }
        public CommandViewModel SettingsCommand
        {
            get
            {
                if (settingsCommand == null)
                {
                    settingsCommand = new CommandViewModel(Resources.MainWindow_Command_Settings,
                                                            new DelegateCommand(param => ShowSettings()));
                }
                return settingsCommand;
            }
        }
        #endregion

        #region "Commands Methods"
        void ShowOutboxRecord()
        {
            Messenger.Default.Send(new NotificationMessage(ViewsNames.OutboxRecordViewName));
            //this.Workspaces.Add(record);
            //SetWorkspace(record);
        }
        void ShowAllOutboxes()
        {
            var workspace = Workspaces.FirstOrDefault(vm => vm is AllOutboxesViewModel) as AllOutboxesViewModel;
            if (workspace == null)
            {
                workspace = new AllOutboxesViewModel();
                Workspaces.Add(workspace);
            }
            SetWorkspace(workspace);
        }
        void ShowBulkOutboxes() 
        {
            var workspace = Workspaces.FirstOrDefault(vm => vm is BulkOutboxViewModel) as BulkOutboxViewModel;
            if (workspace == null)
            {
                workspace = new BulkOutboxViewModel();
                Workspaces.Add(workspace);
            }
            SetWorkspace(workspace);
        }
        void ShowSettings() 
        {
            var workspace = Workspaces.FirstOrDefault(vm => vm is SettingsViewModel) as SettingsViewModel;
            if (workspace == null)
            {
                workspace = new SettingsViewModel();
                Workspaces.Add(workspace);
            }
            SetWorkspace(workspace);
        }
        #endregion

        #region "Properties"

        public ReadOnlyCollection<CommandViewModel> CommonTaksCommands
        {
            get
            {
                if (commands == null)
                {
                    List<CommandViewModel> cmd = AddCommonTasksCommands();
                    commands = new ReadOnlyCollection<CommandViewModel>(cmd);
                }
                return commands;
            }
        }

        private List<CommandViewModel> AddCommonTasksCommands()
        {
            return new List<
            CommandViewModel>
            { 
             OutboxRecordCommand,
             AllOutboxesCommand,
            };
        }
        
        #endregion 

        public ObservableCollection<WorkspaceViewModel> Workspaces
        {
            get
            {
                if (workspaces == null)
                {
                    workspaces = new ObservableCollection<WorkspaceViewModel>();
                    workspaces.CollectionChanged += OnWorkspacesChanged;
                }
                return workspaces;
            }
        }
        void OnWorkspacesChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count != 0)
            {
                foreach (WorkspaceViewModel workspace in e.NewItems)
                {
                    workspace.RequestClose += this.OnWorkspaceRequestClose;
                }
            }
            if (e.OldItems != null && e.OldItems.Count != 0)
            {
                foreach (WorkspaceViewModel workspace in e.OldItems)
                {
                    workspace.RequestClose -= this.OnWorkspaceRequestClose;
                }
            }
        }
        void OnWorkspaceRequestClose(object sender, EventArgs e)
        {
            WorkspaceViewModel workspace = sender as WorkspaceViewModel;
            if (workspace.ModelHasChanges)
            {
                if (! Helper.ShowMessage(Resources.PrompetToSaveMsg, MessagePrompetType.AskQuestion))
                {
                    return;
                }
            }
            workspace.Cleanup();
            Workspaces.Remove(workspace);
           
        }
        void SetWorkspace(WorkspaceViewModel ws)
        {
            ICollectionView cv = CollectionViewSource.GetDefaultView(Workspaces);
            if (cv != null)
            {
                cv.MoveCurrentTo(ws);
            }
        }
    }
}
